#include "Headers.h"
#include "DataHandler.h"
#include "utils.h"

using namespace std;

void handle_command(string);

void handle_command(string cmd, DataHandler &handler) {
    vector<string> cmd_split;
    cmd_split = split(cmd , ' ');
    QUERY_TYPE op_type;

    string key1, key2, op;
    key1 = cmd_split[0];
    op = cmd_split[1];
    key2 = cmd_split[2];

    if (op == "AND") {
        op_type = INTERSECTION;
    }
    else if (op == "OR") {
        op_type = UNION;
    }
    else if (op == "NOT") {
        op_type = DIFFERENCE;
    }

    handler.query_print( op_type, key1, key2);
}


int main(int argc, char *argv[]){
    string cmd;
    istream& is(cin);
	if(argc != 2){
		std::cerr << "Parameter error." << std::endl;
		return 0;
	}

	ifstream ifs(argv[1]);

	if( ifs.fail() ){
		std::cerr << "File open error." << std::endl;
		return 0;
	}

    DataHandler datahandler(ifs);
    ifs.close();

    while ( is.good() ) {
        getline(is, cmd);
        if ( !is.good() ) break;
        cmd.erase(cmd.end()-1);

        handle_command(cmd, datahandler);
    }


	return 0;
}
